const { devDebug } = require("../settings");
import { coupon_state_map } from './db_data'

async function search(searchText, searchData) {
  console.log('搜索字段：' + searchText);
  const resList = await new Promise(resolve => {
    wx.cloud.callFunction({
      name: 'db_coupon',
      data: {
        funcName: 'query',
        searchData: searchData,
      },
      success: (r) => resolve(r.result),
    });
  })
  if (!devDebug) console.log('卡券搜索结果', resList);
  return resList.map(o => {
    o.start_time = new Date(o.start_time).Format()
    o.end_time = new Date(o.end_time).Format()
    o.price = '¥' + (o.price.toFixed(2));
    o.coupon_state_text = coupon_state_map.get(o.coupon_state)
    return o;
  })
}

async function viewCoupon(couponId, orderId) {
  wx.navigateTo({
    url: `/pages/user/coupon/list?order_id=${orderId}`,
  })
}

async function viewCouponChange(couponId, orderId) {
  wx.navigateTo({
    url: `/pages/templates/404?errMsg=暂未实现`,
  })
}

module.exports = {
  title: '_id',
  subtitle: 'coupon_state_text',
  data_keys: [
    { k: 'start_time', v: '生效时间' },
    { k: 'end_time', v: '失效时间' },
    { k: 'price', v: '支付金额' },
    { k: 'order_id', v: '订单编号' },
  ],
  data_btns: [
    { label: '订单核销', funcName: 'viewCoupon' },
    // { label: '核销变动', funcName: 'viewCouponChange' },
  ],
  search_placeholder: '搜索不可用',
  header_btn: { text: '导出核销码', url: '/pages/templates/404?errMsg=暂未实现' },
  tabs: [
    { label: '全部', searchData: {} },
    { label: '待核销', searchData: { coupon_state: 0 } },
    { label: '已核销', searchData: { coupon_state: 1 } },
    { label: '已退款', searchData: { coupon_state: 2 } },
    { label: '已过期', searchData: { coupon_state: 3 } },
  ],
  viewCoupon,
  viewCouponChange,
  search,
}